BEGIN TRANSACTION;  -- 开启事务
BEGIN TRY
    DECLARE @FromAccountID VARCHAR(20) = 'A123456';
    DECLARE @ToAccountID VARCHAR(20) = 'B789012';
    DECLARE @Amount DECIMAL(18, 2) = 100.00;

    -- 验证转出账户存在且余额充足
    IF NOT EXISTS (SELECT 1 FROM BankAccounts WHERE AccountID = @FromAccountID)
        THROW 50001, '转出账户不存在', 1;

    IF (SELECT Balance FROM BankAccounts WHERE AccountID = @FromAccountID) < @Amount
        THROW 50002, '账户余额不足', 1;

    -- 扣减转出账户余额
    UPDATE BankAccounts 
    SET Balance = Balance - @Amount,
        LastUpdateTime = GETDATE()
    WHERE AccountID = @FromAccountID;

    -- 增加转入账户余额
    UPDATE BankAccounts 
    SET Balance = Balance + @Amount,
        LastUpdateTime = GETDATE()
    WHERE AccountID = @ToAccountID;

    COMMIT TRANSACTION;  -- 提交事务
    PRINT '转账成功';
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;  -- 回滚事务
    PRINT '转账失败：' + ERROR_MESSAGE();  -- 输出错误信息
END CATCH;
